package com.itheima.exception;

import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @Description:
 * @Version: V1.0
 */
@Component
public class ExceptionResolver implements HandlerExceptionResolver {
    @Override
    public ModelAndView resolveException(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) {
/*        System.out.println("异常处理器执行中...");
        ModelAndView modelAndView = new ModelAndView();
        //定义异常现象出现后,反馈给用户看的信息
        modelAndView.addObject("msg","wrong");
        //定义异常出现后,反馈给用户查看的页面
        modelAndView.setViewName("error.jsp");*/

        ModelAndView modelAndView = new ModelAndView();
        if(e instanceof NullPointerException){
            modelAndView.addObject("msg","空指针异常");
        }else if (e instanceof ArithmeticException){
            modelAndView.addObject("msg","算数运算异常");
        }else{
            modelAndView.addObject("msg","未知的异常");
        }
            modelAndView.setViewName("error.jsp");
        return modelAndView;
    }
}
